package one.chchy.demo.sprintsecurity;

import one.chchy.demo.sprintsecurity.util.EncryptionUtil;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class MD5PasswordEncoder implements PasswordEncoder {

    @Override
    public String encode(CharSequence charSequence) {
        try {
            return EncryptionUtil.strToMD5(charSequence.toString());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return UUID.randomUUID().toString();
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {

        String postPassword = UUID.randomUUID().toString();

        try {
            postPassword = EncryptionUtil.strToMD5(charSequence.toString());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return s.equals(postPassword);
    }
}
